/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.raflik.queryxml.xpath1;

import java.util.List;
import pl.raflik.queryxml.langservice.GenericJavaxXPathApiResolver;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathFactoryConfigurationException;
import org.w3c.dom.NodeList;
import pl.raflik.queryxml.langservice.ConvUtil;
import pl.raflik.queryxml.langservice.QueryResolverException;

/**
 *
 * @author raflik
 */
public class DefaultXPathResolver extends GenericJavaxXPathApiResolver {

    private static final String LANG_XPATH1 = "XPath 1.0 (Default)";
    private static final String MIME_TYPE = "text/x-xpath1";

    @Override
    public String supportedLanguage() {
        return LANG_XPATH1;
    }

    protected XPath instantiateXPath() throws XPathFactoryConfigurationException {
        return XPathFactory.newInstance(XPathFactory.DEFAULT_OBJECT_MODEL_URI).newXPath();
    }

    public List<?> resolve(String query, Object node, Object param) throws QueryResolverException {
        List<?> ret = null;
        Object result = genericResolve(query, node, param);
        if (result instanceof NodeList) {
            NodeList nodeList = (NodeList) result;
            ret = ConvUtil.toList(nodeList);
        }
        return ret;

    }

    public String mimeType() {
        return MIME_TYPE;
    }
}
